#coding:utf-8
from settings import GROUP_PAGE_SIZE,DEBUG
from django.db import connection 
from django.core.cache import cache
import memcache
#from __future__ import division
import os
import Image
import logging

mclient = memcache.Client(['127.0.0.1:11211'], debug=0)
  
def debug_sql(op=''):
    if DEBUG:
        infos = connection.queries
        print "\n",'*'*20,u'%s 数据库查询信息'%op,'*'*20
        for info in infos:
            print u'耗费时间：%s'%info.get('time')
            print u'执行脚本：%s'%info.get('sql')
            print '*'*60


def get_page(request):
    try:
        return int(request.GET.get('page', '1'))
    except ValueError:
        return 1       
    
def make_thumb(data, size = 120):
    data.seek(0)
    pixbuf = Image.open(data)
    width, height = pixbuf.size

    if width > size:
        delta = width / size
        height = int(height / delta)
        pixbuf.thumbnail((size, height), Image.NEAREST)

    return pixbuf    


def cache_get(key,new_data=None,expire=60*60*24*30):
    val = mclient.get(key)
    if val is not None:
        logging.info(u"cache hit::%s"%key)
        return val
    else:
        logging.info(u"cache not hit::%s"%key)
        mclient.set(key,new_data,expire)
    return new_data


    
    
    
    
    
    
    
    
    
    
    